package com.mes.task.service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.service.BasicService;
import com.mes.common.util.SysUtil;
import com.mes.task.dao.iface.IOperationTaskDao;
import com.mes.task.dao.iface.IPartTaskDao;
import com.mes.task.dao.iface.IPartTaskLinkDao;
import com.mes.task.domain.OperationTask;
import com.mes.task.domain.PartTask;
import com.mes.task.manager.iface.IPartTaskManager;
import com.mes.util.ScheduleUtil;

@Service
public class PartTaskService extends BasicService<OperationTask> {

	@Autowired
	SysUtil sysUtil;
	@Autowired
	IOperationTaskDao operationTaskDao;
	@Autowired
	IPartTaskDao partTaskDao;
	@Autowired
	IPartTaskManager partTaskManager;
	@Resource
	private IPartTaskLinkDao partTaskLinkDao;
	
	@Resource
	private ScheduleUtil scheduleUtil;
	
	/**
	 * @author lcs
	 * @see 用于AO任务管理界面（AOTaskManger）界面主dataGrid获取数据
	 * @param page
	 * @param params
	 * @throws Exception
	 */
	@DataProvider
	public void getAOTaskByTaskId(Page<PartTask> page, Map<String, Object> parameters) throws Exception {

		String taskId = (String) parameters.get("taskId");
		String drawingId = (String) parameters.get("drawingId");
		String batchNum = (String) parameters.get("batchNum");
		Integer taskState=null;
		if(parameters.get("taskState")==null){
			taskState = (Integer)parameters.get("taskState");
		}else{
			taskState =Integer.parseInt(((String)parameters.get("taskState")));
		}
		// 实现按照图号、批次、状态进行过滤 hba
		//Page page = new Page(dataSet.getPageIndex(), dataSet.getPageSize());
		List<PartTask> list = partTaskDao.getAOTasksByPartTaskId(taskId,
				drawingId, batchNum, taskState, page);
		if (list != null) {
			for (PartTask partTask : list) {
				String preTaskName = "";
				List<PartTask> prePartTaskList = partTaskLinkDao
						.getPredPartTasks(partTask.getTaskUid());
			}
		}
				scheduleUtil.initPage(page, list);
				
				/*if (prePartTaskList != null) {
					for (PartTask partTask2 : prePartTaskList) {
						preTaskName += partTask2.getTaskName() + ",";
					}
					if (preTaskName != "") {
						partTask.getTempObject().setText1(
							preTaskName.substring(0,
										preTaskName.length() - 1));
					} else {
						partTask.getTempObject().setText1(preTaskName);
					}
				}
			}
*/
			//super.setupDataset(dataSet, list, page);
		
	}
	

		


	
	/**
	 * 根据零件任务唯一号获取零件任务
	 * @param partTaskUid
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public List<PartTask> getPartTasksByPartTaskUid(String partTaskUid)
			throws Exception {
		List<PartTask> listPartTask = new ArrayList<PartTask> ();
		PartTask partTask = partTaskDao.getById(partTaskUid);
		if(partTask != null){
			listPartTask.add(partTask);
		}
		return listPartTask;
	}

	/**
	 * 根据物料唯一号获取零件任务
	 * @param partUid
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public List<PartTask> getPartTasksByPartUid(String partUid)
			throws Exception {
		List<PartTask> listPartTask = partTaskDao.findByProperty("partUid", partUid, "batchNum");
		return listPartTask;
	}
	
	/**
	 * 获取未分解的零件任务
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public void getUnBreakedPartTasks(Page<PartTask> page)
			throws Exception {
		partTaskDao.getUnBreakPartTasks(page);
	}
	
	
	@DataResolver
	public void taskBreak(PartTask partTask) throws Exception{
		System.out.println(partTask.getUuid());
		String aaa = sysUtil.getProperty("coSchedule");
		
//		partTaskManager.partTaskBreak(partTask);
	}
	
	@DataProvider
	public void getPartTaskByCondition(Page<PartTask> page, Map<String, Object> params){
		try {
			partTaskDao.getPartTaskByCondition(page, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
